// pages/nearMech/nearMech.js
var app=getApp();
var util=require("../../utils/util");
var home=require("../../api/home");
var config=require("../../api/config");


Page({
  /* 页面的初始数据*/
  data: {
    address_info: '',
    mech_show: true,
    top: '',
    bar_index: 1,
    area_list: [],
    cate_list: [],
    cur_index: 0,
    cur_index2: 0,
    cate_name: '',
    show_page: 1,
    mech_list: [],
    currentPage: 1,
    pageSize: 10,
    hasMoreData: true,
    show_nav: false
  },
  /*生命周期函数--监听页面加载*/
  onLoad: function (options) {
    var that=this;
    wx.getStorage({
      key: 'city',
      success:function(res){
        that.setData({address_info:res.data})
        //
        that.get_mech_list(res.data,'');
        that.get_area_list(res.data.city_code)
      },
    })
    that.get_cate_list();
  },
  // 跳转搜索
  nav_search:function(){
    wx.navigateTo({
      url: '../search/search',
    })
  },
  // 顶部切换
  check_bar:function(e){
    var that=this,mech_show=that.data.mech_show,bar_index=that.data.bar_index;
    var index = e.currentTarget.dataset.index;
    if(bar_index == index){
      that.setData({bar_index: index,mech_show:!that.data.mech_show});
      return false;
    }
    if(mech_show){
      that.setData({bar_index: index,mech_show:!that.data.mech_show,show_page:index});
    }
    // 事件统计
    app.td_app_sdk.event({
      id: 'mechChangeBar',
      label: '类型和区域选择',
      params: {
        key1: index==1?'类型':'区域'
      }
    })
  },
  // 关闭弹窗
  close_mech:function(){
    this.setData({mech_show:!this.data.mech_show})
  },
  // 选择区域和类型
  check_mech_item:function(e){
    var that=this,show_page=that.data.show_page,address_info=that.data.address_info;
    var index=e.currentTarget.dataset.index,item=e.currentTarget.dataset.item;
    if(show_page==2){
      address_info.district=item.areaId=="areaId"?'':item.areaName;
      that.setData({cur_index:index,address_info:address_info})
    }else{
      var cate_name=item.name;
      that.setData({cur_index2:index,cate_name:cate_name})
    }
  },
  confirm_mech:function(){
    var that=this,address_info=that.data.address_info,cate_name=that.data.cate_name;
    that.setData({mech_list:[],currentPage:1,show_nav: false})
    that.get_mech_list(address_info,cate_name);
    that.close_mech();
  },
  // 附近机构地图
  nav_map:function(){
    wx.navigateTo({
      url: '../nearMap/nearMap',
    })
  },
  return:function(){},
  // 触底
  onReachBottom:function(){
    var that=this,hasMoreData=that.data.hasMoreData,address_info=that.data.address_info,cate_name=that.data.cate_name;
    if(hasMoreData){
      that.get_mech_list(address_info,cate_name)
    }else{
      util.page_show_toast('没有更多了',1500);
    }
  },
  onReady: function () {
    var that = this;
    var query = wx.createSelectorQuery();
    query.selectAll('.top').boundingClientRect(function (res) {
      that.setData({top: res[0].height})
    }).exec()
  },
  // 获取分类
  get_cate_list:function(){
    var that=this;
    home.get_home_classify_list().then(res=>{
      if(res.data.code==200){
        var list=res.data.data;
        for(var i=0;i<list.length;i++){
          if(list[i].name=="附近机构"){
            list.splice(i,1)
          }
        }
        var all=[{id:0,name:"全部"}];
        list=util.extend(all,list)
        that.setData({cate_list:list})
      }
    })
  },
  // 机构列表
  get_mech_list:function(info,cate_name){
    wx.showLoading({title: '加载中'})
    var that=this,currentPage=that.data.currentPage,pageSize=that.data.pageSize,scope=wx.getStorageSync('scope'),cur_index=that.data.cur_index;
    var params={
      "lat": info.lat,
      "lng": info.lng,
      "currentPage": currentPage,
      "pageSize": pageSize,
      "province": info.province,
      "city": info.city,
      "region": cur_index>0?info.district:'',
      "status": 2,
      "categoryParentNames": cate_name=='全部'?'':cate_name,
      "name": '',
      "range": scope?(scope*1000):''
    }
    home.get_mech_list(params).then(res=>{
      if(res.data.code==200){
        var list = res.data.data.records;
        for(var i=0;i<list.length;i++){
          list[i].score=list[i].score?list[i].score.toFixed(1):'5.0';
          list[i].space=list[i].distance>1000?(list[i].distance/1000).toFixed(2):list[i].distance;
        }
        var mech_list = that.data.mech_list;
        mech_list = util.extend(mech_list, list)
        if (list.length < pageSize) {
          that.setData({
            mech_list: mech_list,
            hasMoreData: false
          })
        } else {
          that.setData({
            mech_list: mech_list,
            hasMoreData: true,
            currentPage: currentPage + 1
          })
        }
        if(list.length==0&&mech_list.length==0){
          that.setData({show_nav:true})
        }
      }
    })
  },
  // 获取区域
  get_area_list:function(id){
    var that=this,address_info=that.data.address_info,index=0;
    config.get_area_list({"pid":id}).then(res=>{
      if(res.data.code==200){
        var list=res.data.data;
        var all=[{areaId:"areaId",areaName:"全部"}];
        list=util.extend(all,list);
        // for(var i=0;i<list.length;i++){
        //   if(list[i].areaCode==address_info.adcode){
        //     index=i
        //   }
        // }
        // that.setData({area_list: list,cur_index:index})
        that.setData({area_list:list})
      }
    })
  },
})